﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using EPPlus.Core.Extensions;
using Hicap.AuthorizationServer.CommonDto;

namespace Hicap.AuthorizationServer.Helper
{
    public static class ExcelHelper
    {
        public static List<ExcelTableColumnAttributeAndProperyInfo> GetExcelTableColumnAttributesWithProperyInfo(this Type type)
        {
            List<ExcelTableColumnAttributeAndProperyInfo> columnAttributesWithProperyInfo = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Select(property => new ExcelTableColumnAttributeAndProperyInfo(property, property.GetCustomAttributes(typeof(ExcelTableColumnAttribute), true).FirstOrDefault() as ExcelTableColumnAttribute))
                .Where(p => p.ColumnAttribute != null)
                .ToList();

            if (!columnAttributesWithProperyInfo.Any())
            {
                throw new ArgumentException($"Given object does not have any {nameof(ExcelTableColumnAttribute)}.");
            }

            return columnAttributesWithProperyInfo;
        }
    }
}